package com.quanyan.club.thread;

import com.alibaba.fastjson.JSON;
import com.quanyan.api.APIResponse;
import com.quanyan.club.entity.RespClubBaseInfo;
import com.quanyan.club.util.ClubNameUtils;
import com.quanyan.common.utils.StringUtil;
import com.quanyan.common.vo.resp.RespCity;
import com.quanyan.common.vo.resp.RespDistrict;
import com.quanyan.search.entity.vo.ClubIndexInfo;
import com.quanyan.search.service.impl.ClubSearchServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by zhuo.shi on 2016/10/20.
 */

public class ClubSolrIndexThread implements  Runnable{

    private Logger logger = LoggerFactory.getLogger(ClubSolrIndexThread.class);

    private boolean isUpdate;     //1 插入 2 更新

    private RespClubBaseInfo clubBaseInfo;
    private int clubId;
    private ClubSearchServiceImpl clubSearchService;


    public ClubSolrIndexThread(RespClubBaseInfo clubBaseInfo, ClubSearchServiceImpl clubSearchService, boolean isUpdate){
        this.clubBaseInfo = clubBaseInfo;
        this.clubSearchService = clubSearchService;
        this.isUpdate = isUpdate;
    }

    @Override
    public void run() {

        Integer clubId = clubBaseInfo.getClubId();
        //先删除
        long t2 = System.currentTimeMillis();
        //先删除
        APIResponse response = clubSearchService.deleteDocById(clubId);
        if (!response.isRet()){
            logger.warn("删除俱乐部"+ clubId +"索引，"+ response.getErrmsg());
        }
        logger.warn("删除俱乐部clubId={}索引，花费：{}", clubId, System.currentTimeMillis()-t2);

        long t1 = System.currentTimeMillis();
        ClubIndexInfo indexInfo = new ClubIndexInfo();
        indexInfo.setClub_id(String.valueOf(clubBaseInfo.getClubId()));
        indexInfo.setClub_name(clubBaseInfo.getClubName());
        String clubNamePY = ClubNameUtils.getClubNamePinYin(clubBaseInfo.getClubName());
        indexInfo.setClub_name_pinyin(clubNamePY);
        if (!StringUtil.isBlankOrNull(clubNamePY)) {
            indexInfo.setClub_name_first_letter(clubNamePY.substring(0, 1));
        }

        RespCity city = clubBaseInfo.getCity();
        if (city != null) {
            indexInfo.setClub_city_id(city.getId());
            indexInfo.setClub_city_name(city.getName());
        }
        RespDistrict district = clubBaseInfo.getDistrict();
        if (district != null){
            indexInfo.setClub_district_id(district.getId());
            indexInfo.setClub_district_name(district.getName());
        }
        indexInfo.setClub_logo(clubBaseInfo.getClubLogo());
        indexInfo.setClub_founder_nickname(clubBaseInfo.getUsername());
        indexInfo.setClub_founder_uid(clubBaseInfo.getUid());
        indexInfo.setClub_sport_name(clubBaseInfo.getSportName());
        if (clubBaseInfo.getStar() != null) {
            indexInfo.setClub_star(clubBaseInfo.getStar());
        }
        indexInfo.setClub_integral(clubBaseInfo.getIntegral()==null?0:clubBaseInfo.getIntegral());
        indexInfo.setClub_level(clubBaseInfo.getLevel()==null?0:clubBaseInfo.getLevel());
        indexInfo.setClub_type_id(clubBaseInfo.getClubType());
        Integer placeId = clubBaseInfo.getResidentPlaceId();
        indexInfo.setClub_lat(clubBaseInfo.getLat() == null? 0.0: clubBaseInfo.getLat());
        indexInfo.setClub_lng(clubBaseInfo.getLng() == null? 0.0: clubBaseInfo.getLng());
        if (clubBaseInfo.getGoingActivityCount() != null) {
            indexInfo.setClub_activity_count(clubBaseInfo.getGoingActivityCount());
        }
        logger.warn("clubIndex=" + JSON.toJSONString(indexInfo));

        APIResponse response2 = clubSearchService.insertDoc(indexInfo);
        if (response2 == null || !response2.isRet()){
            logger.warn("添加俱乐部索引失败：clubId={}", clubBaseInfo.getClubId());
            logger.warn("club index info:" + JSON.toJSONString(clubBaseInfo) + "，返回：" + JSON.toJSON(response2));
        }else {
            logger.debug("添加俱乐部clubId={}索引成功", clubBaseInfo.getClubId());
        }

        logger.warn("添加俱乐部clubId={}索引，花费：{}", clubBaseInfo.getClubId(), System.currentTimeMillis()-t1);
    }

}
